package com.gongpingjialoan.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.gongpingjialoan.R;
import com.gongpingjialoan.adapter.car.BrandAdapter;
import com.gongpingjialoan.data.NewCarBrand;

import java.util.List;

/**
 * Created by Administrator on 2016/5/20.
 */
public class BrandPopupWindow extends PopupWindow implements AdapterView.OnItemClickListener {


    private Context mContext;

    private View mMenuView;

    private ListView listView;

    private List<NewCarBrand> mBrandBean;

    private BrandAdapter brandAdapter;

    private ISelctedPrice mISelctedPrice;


    public void setISelctedPrice(ISelctedPrice ISelctedPrice) {
        mISelctedPrice = ISelctedPrice;
    }

    public BrandPopupWindow(Context context) {
        super(context);
        this.mContext = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mMenuView = inflater.inflate(R.layout.sort_popup, null);

        listView = (ListView) mMenuView.findViewById(R.id.listview);
        mMenuView.findViewById(R.id.price_popup).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        this.setContentView(mMenuView);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        //因为某些机型是虚拟按键的,所以要加上以下设置防止挡住按键.、
        this.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        listView.setOnItemClickListener(this);
        ColorDrawable cd = new ColorDrawable(0x000000);
        this.setBackgroundDrawable(cd);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        brandAdapter.setPosition(position);
        mISelctedPrice.select(mBrandBean.get(position).getBrand(),mBrandBean.get(position).getBrand_slug());
        dismiss();
    }

    public void setBrands(List<NewCarBrand> brandBean) {
        mBrandBean = brandBean;
        brandAdapter = new BrandAdapter(mBrandBean, mContext);
        listView.setAdapter(brandAdapter);
        brandAdapter.setPosition(-1);
    }


    public interface ISelctedPrice {
         void select(String brand,String brand_slug);
    }
}
